﻿using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.GraphicsInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;

namespace CADTool
{
    public class JigListDBtext : DrawJig
    {
        public List<DBText> m_DBText = new List<DBText>();
        private Point3d m_Pt1;
        public JigListDBtext(Point3d position,List<DBText> dBTextList)
        {
            m_DBText = dBTextList;
            m_Pt1 = position;
        }
        public List<Entity> GetEntity()
        {
            List<Entity> list = new List<Entity>();
            foreach (var item in m_DBText)
            {
                if (item is Entity txt)
                {
                    list.Add(txt);
                }
            }
            return list;
        }
        protected override SamplerStatus Sampler(JigPrompts prompts)
        {
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            Matrix3d mt = ed.CurrentUserCoordinateSystem;
            //定义一个点拖拽交互类
            JigPromptAngleOptions optJigPoint= new JigPromptAngleOptions("\n请指定文字放置位置");
            //光标类型
            optJigPoint.Cursor = CursorType.Crosshair;
            //拖拽限制
            optJigPoint.UserInputControls = UserInputControls.Accept3dCoordinates | UserInputControls.NoZeroResponseAccepted |
                UserInputControls.NoNegativeResponseAccepted;
            //拖拽基点必须是WCS点
            //optJigPoint.BasePoint=m_Pt1.TransformBy(mt);
            //optJigPoint.UseBasePoint= true;
            PromptPointResult resJigPoint = prompts.AcquirePoint();
            Point3d tempPt = resJigPoint.Value;     
            if (resJigPoint.Status==PromptStatus.Cancel)
            {
                return SamplerStatus.Cancel;
            }
            if (m_Pt1!=tempPt)
            {
                m_Pt1 = tempPt;
                //更新文字位置              
                for (int i = 0; i < m_DBText.Count; i++)
                {
                    Vector3d tempVec=new Vector3d(0,i* m_DBText[0].Height*1.8, 0);
                    m_DBText[i].Position=m_Pt1+ tempVec;
                }
                return SamplerStatus.OK;
            }
            return SamplerStatus.NoChange;
        }

        protected override bool WorldDraw(WorldDraw draw)
        {
            foreach (var item in m_DBText)
            {
                draw.Geometry.Draw(item);
            }
            return true;
        }
    }
}
